package com.xiyi.shouxin.controller;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.xiyi.shouxin.controller.pojo.ListPracticeParams;
import com.xiyi.shouxin.entity.admin.Admin;
import com.xiyi.shouxin.entity.user.Practice;
import com.xiyi.shouxin.entity.user.User;
import com.xiyi.shouxin.exception.ErrorCode;
import com.xiyi.shouxin.init.CacheUtil;
import com.xiyi.shouxin.service.PracticeService;
import com.xiyi.shouxin.utils.ExceptionUtil;
import com.xiyi.shouxin.view.CommonModel;
import com.xiyi.shouxin.view.pojo.CprListView;
import com.xiyi.shouxin.view.pojo.CprView;
import com.xiyi.shouxin.view.pojo.ItemPage;
import com.xiyi.shouxin.view.pojo.PracticeView;
import com.xiyi.shouxin.view.pojo.UserPracticeTimeView;

@CrossOrigin("*")
@RequestMapping("/practice")
@RestController
public class PracticeController {
    private Gson gson = new Gson();
    @Autowired
    private Logger logger;
    @Autowired
    private CacheUtil cacheUtil;
    @Autowired
    private PracticeService practiceService;

    @PostMapping("/list_practice")
    public CommonModel listPractice(@RequestBody ListPracticeParams params) {
        if (params == null || params.getPage() == null || params.getSize() == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        User user = cacheUtil.getUserOperator();
        Long uid=params.getUserId();
        if (user != null) {
            uid=user.getId();
        }
        Long adminId=null;
        Admin admin=cacheUtil.getAdminOperator();
        if(admin!=null){
            adminId=admin.getId();
        }
        try {
            ItemPage<PracticeView> info = practiceService.findPracticeList(adminId,params.getPiciId(),params.getPids(),params.getKeyword(),uid, params.getStartTime(),
                    params.getEndTime(), params.getPage(), params.getSize(),params.getSort(),params.getOrder());
            return CommonModel.Success(info);
        } catch (Exception e) {
            logger.error("{}查询练习列表{}异常：", gson.toJson(cacheUtil.getAdminOperator()), gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/info")
    public CommonModel practiceInfo(@RequestBody Practice params) {
        if (params == null || params.getId() == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        try {
            CprListView view=new CprListView();
            PracticeView info = practiceService.findPractice(params.getId());
            if(info==null){
                return CommonModel.Success(view);
            }
            ItemPage<CprView> list =practiceService.findCprList(info.getUid(), params.getId(), 1, 2200);
            view.setCprList(list.getList());
            view.setPractice(info);
            return CommonModel.Success(view);
        } catch (Exception e) {
            logger.error("查询练习详情{}异常：", gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/info_pici")
    public CommonModel practiceInfoPici(@RequestBody Practice params) {
        if (params == null || params.getId() == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        try {
            CprListView view=new CprListView();
            PracticeView info = practiceService.findPracticeWithHeavy(params.getId());
            if(info==null){
                return CommonModel.Success(view);
            }
            ItemPage<CprView> list =practiceService.findCprList(info.getUid(), params.getId(), 1, 2200);
            view.setCprList(list.getList());
            view.setPractice(info);
            return CommonModel.Success(view);
        } catch (Exception e) {
            logger.error("查询练习详情{}异常：", gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/detail")
    public CommonModel practiceDetail(@RequestBody Practice params) {
        User user = cacheUtil.getUserOperator();
        if (user == null) {
            return CommonModel.Fail(ErrorCode.INVALIDTOKEN.getCode(), "请重新登录", null);
        }
        if (params == null || params.getId() == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        try {
            CprListView view=new CprListView();
            ItemPage<CprView> list =practiceService.findCprList(user.getId(), params.getId(), 1, 2200);
            PracticeView info = practiceService.findPractice(params.getId());
            view.setCprList(list.getList());
            view.setPractice(info);
            return CommonModel.Success(view);
        } catch (Exception e) {
            logger.error("查询练习详情{}异常：", gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/user_time")
    public CommonModel practiceTime() {
        User user = cacheUtil.getUserOperator();
        if (user == null) {
            return CommonModel.Fail(ErrorCode.INVALIDTOKEN.getCode(), "请重新登录", null);
        }
        try {
            UserPracticeTimeView info = practiceService.sumUserTime(user.getId());
            return CommonModel.Success(info);
        } catch (Exception e) {
            logger.error("查询用户练习时长异常：", e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }
}
